<?php

class Agenda extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('admin/m_agenda');
        $this->load->library('pagination');
		$this->load->helper('tgl_indonesia');
		

	}
	
	function index(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_agenda->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/agenda/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$data['tabel'] = $this->m_agenda->getTable($config['per_page'], $this->uri->segment(4));
			$data['main_view'] = 'admin/agenda/index';
			$this->load->view('admin/template', $data);
		}
	}
	
	function add(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			//$data['scriptmce'] = $this->scripttiny_mce();
			$data['main_view'] = 'admin/agenda/add';
			$this->load->view('admin/template', $data);
		}
	}
	
	function insert(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('tema', 'Judul Berita', 'required');
			$this->form_validation->set_rules('isi_agenda', 'Isi Berita', 'required');
			$this->form_validation->set_rules('mulai', 'Tanggal Mulai', 'required');
			$this->form_validation->set_rules('selesai', 'Tanggal Selesai', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			if($this->form_validation->run()==true){
				$tema = $this->input->post('tema');
				$isi_agenda = $this->input->post('isi_agenda');
				$tempat = $this->input->post('tempat');
				$tgl_mulai = $this->input->post('mulai');
				$tgl_selesai = $this->input->post('selesai');
				$tgl = date("Y-m-d");
				$hari = nama_hari($tgl);
				$seo = str_replace(" ", "-", $tema);
				$tema_seo = strtolower($seo . ".html");
				$gbr=$_FILES['userfile']['name'];
				$acak=rand(00000000000,99999999999);
				$gbr_default = 'null';
				$gbr_bru = $acak.$gbr;
				
				if(!empty($gbr)){
					$config['upload_path'] = './image/gambar_event/';
					$config['allowed_types'] = 'bmp|gif|jpg|jpeg|png';
					$config['max_size'] = '2000';
					$config['max_width'] = '1020';
					$config['max_height'] = '960';	
					$config['file_name']= $gbr_bru;				
					$this->load->library('upload', $config);
				
					if(!$this->upload->do_upload())
					{
						echo $this->upload->display_errors();
					}else{
						$this->m_agenda->create($tema,$isi_agenda,$tempat,$tgl_mulai,$tgl_selesai,$tgl,$gbr_bru,$tema_seo);
						redirect('admin/agenda');
					}
				}else{
					$this->m_agenda->create($tema,$isi_agenda,$tempat,$tgl_mulai,$tgl_selesai,$tgl,gbr_default,$tema_seo);
					redirect('admin/agenda');
				}
				
			}else{
				$data['main_view'] = 'admin/agenda/add';
				$this->load->view('admin/template', $data);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function edit(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			//$id = $this->uri->segment();
			$data['data'] = $this->m_agenda->getDataById($this->uri->segment(4));
			//$data['scriptmce'] = $this->scripttiny_mce();
			$data['main_view'] = 'admin/agenda/edit';
			$this->load->view('admin/template', $data);
		}
	}
	
	function update($id){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$this->load->library('form_validation');
		
			$this->form_validation->set_rules('tema', 'Judul Berita', 'required');
			$this->form_validation->set_rules('isi_agenda', 'Isi Berita', 'required');
			$this->form_validation->set_rules('mulai', 'Tanggal Mulai', 'required');
			$this->form_validation->set_rules('selesai', 'Tanggal Selesai', 'required');
			$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
			
			if($this->form_validation->run()==true){
				$tema = $this->input->post('tema');
				$isi_agenda = $this->input->post('isi_agenda');
				$tempat = $this->input->post('tempat');
				$tgl_mulai = $this->input->post('mulai');
				$tgl_selesai = $this->input->post('selesai');
				$id = $this->input->post('id');
				$seo = str_replace(" ", "-", $tema);
				$tema_seo = strtolower($seo . ".html");
				$gbr = $_FILES['userfile']['name'];
				$gbr_lama = $this->input->post('gambar');
				$acak = rand(00000000000,99999999999);
				$gbr_baru = $acak.$gbr;
				
				if(!empty($gbr)){
					$config['upload_path'] = './image/gambar_event/';
					$config['allowed_types'] = 'bmp|gif|jpg|jpeg|png';
					$config['max_size'] = '2000';
					$config['max_width'] = '1020';
					$config['max_height'] = '960';	
					$config['file_name'] = $gbr_baru;			
					$this->load->library('upload', $config);
				
					if(!$this->upload->do_upload())
					{
						echo $this->upload->display_errors();
					}else{
						$file = './image/gambar_event/' . $gbr_lama;
						unlink($file);
						$id = $this->input->post('id');
						$this->m_agenda->update($id,$tema,$isi_agenda,$tempat,$tgl_mulai,$tgl_selesai,$gbr_baru,$tema_seo);
						redirect('admin/agenda');
					}
				}else{
					$this->m_agenda->update($id,$tema,$isi_agenda,$tempat,$tgl_mulai,$tgl_selesai,$gbr_lama,$tema_seo);
					redirect('admin/agenda');
				}
				
			}else{
				$data['main_view'] = 'admin/agenda/add';
				$this->load->view('admin/template', $data);
			}
			//$this->load->view('admin/template', $data);
		}
	}
	
	function delete(){
		$id = $this->uri->segment(4);
		$this->m_agenda->delete($id);
		redirect('admin/agenda');
	}
	
	function search(){
		$sessionData = $this->session->userdata('username');
		if(empty($sessionData)){
			redirect('rootmaster/login');
		}else{
			$table = $this->m_agenda->getAllData();
			
			$config['uri_segment'] = 4;
			$config['total_rows'] = count($table);
			$config['per_page'] = 10;
			$config['base_url'] = base_url() . 'admin/berita/index/';
			
			$this->pagination->initialize($config);
			
			$data['pagination'] = $this->pagination->create_links();
			$judul = $this->input->post('judul');
			
			$data['tabel'] = $this->m_agenda->search($judul);
			$data['main_view'] = 'admin/agenda/index';
			$this->load->view('admin/template', $data);
		}	
	}

}